package org.elte.lipika.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.RequestContext;
import java.util.Set;
import org.elte.lipika.client.managed.request.ApplicationRequestFactory;
import org.elte.lipika.client.managed.request.ExampleProxy;
import org.elte.lipika.client.managed.request.OccurenceProxy;
import org.elte.lipika.client.managed.request.OriginalProxy;
import org.elte.lipika.client.managed.request.TerminusProxy;
import org.elte.lipika.client.managed.request.TerminusRequest;
import org.elte.lipika.client.managed.ui.ExampleSetEditor;
import org.elte.lipika.client.managed.ui.OccurenceSetEditor;
import org.elte.lipika.client.managed.ui.TerminusDetailsView;
import org.elte.lipika.client.managed.ui.TerminusEditView;
import org.elte.lipika.client.managed.ui.TerminusListView;
import org.elte.lipika.client.managed.ui.TerminusMobileDetailsView;
import org.elte.lipika.client.managed.ui.TerminusMobileEditView;
import org.elte.lipika.client.scaffold.ScaffoldApp;
import org.elte.lipika.client.scaffold.place.CreateAndEditProxy;
import org.elte.lipika.client.scaffold.place.FindAndEditProxy;
import org.elte.lipika.client.scaffold.place.ProxyPlace;

public class TerminusActivitiesMapper {

    private final ApplicationRequestFactory requests;

    private final PlaceController placeController;

    public TerminusActivitiesMapper(ApplicationRequestFactory requests, PlaceController placeController) {
        this.requests = requests;
        this.placeController = placeController;
    }

    public Activity getActivity(ProxyPlace place) {
        switch(place.getOperation()) {
            case DETAILS:
                return new TerminusDetailsActivity((EntityProxyId<TerminusProxy>) place.getProxyId(), requests, placeController, ScaffoldApp.isMobile() ? TerminusMobileDetailsView.instance() : TerminusDetailsView.instance());
            case EDIT:
                return makeEditActivity(place);
            case CREATE:
                return makeCreateActivity();
        }
        throw new IllegalArgumentException("Unknown operation " + place.getOperation());
    }

    @SuppressWarnings("unchecked")
    private EntityProxyId<org.elte.lipika.client.managed.request.TerminusProxy> coerceId(ProxyPlace place) {
        return (EntityProxyId<TerminusProxy>) place.getProxyId();
    }

    private Activity makeCreateActivity() {
        TerminusEditView.instance().setCreating(true);
        final TerminusRequest request = requests.terminusRequest();
        Activity activity = new CreateAndEditProxy<TerminusProxy>(TerminusProxy.class, request, ScaffoldApp.isMobile() ? TerminusMobileEditView.instance() : TerminusEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(TerminusProxy proxy) {
                request.persist().using(proxy);
                return request;
            }
        };
        return new TerminusEditActivityWrapper(requests, ScaffoldApp.isMobile() ? TerminusMobileEditView.instance() : TerminusEditView.instance(), activity, null);
    }

    private Activity makeEditActivity(ProxyPlace place) {
        TerminusEditView.instance().setCreating(false);
        EntityProxyId<TerminusProxy> proxyId = coerceId(place);
        Activity activity = new FindAndEditProxy<TerminusProxy>(proxyId, requests, ScaffoldApp.isMobile() ? TerminusMobileEditView.instance() : TerminusEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(TerminusProxy proxy) {
                TerminusRequest request = requests.terminusRequest();
                request.persist().using(proxy);
                return request;
            }
        };
        return new TerminusEditActivityWrapper(requests, ScaffoldApp.isMobile() ? TerminusMobileEditView.instance() : TerminusEditView.instance(), activity, proxyId);
    }
}
